package com.xmhmyh.generalframework.project.systool.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmhmyh.generalframework.common.BaseController;
import com.xmhmyh.generalframework.common.entity.DataGridRequest;
import com.xmhmyh.generalframework.common.entity.Response;
import com.xmhmyh.generalframework.common.enums.LogBusType;
import com.xmhmyh.generalframework.common.enums.LogModule;
import com.xmhmyh.generalframework.common.enums.LogSourceType;
import com.xmhmyh.generalframework.common.utils.QuartzUtils;
import com.xmhmyh.generalframework.framework.aspectj.annotation.Log;
import com.xmhmyh.generalframework.project.systool.conversion.QuartzJob2QuartzJobVOConversion;
import com.xmhmyh.generalframework.project.systool.dto.QuartzJobDTO;
import com.xmhmyh.generalframework.project.systool.entity.QuartzJob;
import com.xmhmyh.generalframework.project.systool.service.IQuartzJobService;
import com.xmhmyh.generalframework.project.systool.vo.QuartzJobVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 定时任务控制器
 *
 * @author zzx
 */
@Api(tags = "定时任务接口")
@RestController
@RequestMapping("/sys/quartz")
public class QuartzJobController extends BaseController {
    @Autowired
    private IQuartzJobService quartzJobService;

    /**
     * 添加定时任务
     *
     * @return
     * @throws SchedulerException
     */
    @Log(busType = LogBusType.EDITANDSAVE, source = LogSourceType.MANAGE, module = LogModule.TASK)
    @ApiOperation(value = "新增定时任务")
    @PreAuthorize("hasAuthority('sys:quartz:save')")
    @PostMapping("/save")
    public Response saveHandler(@RequestBody QuartzJobDTO quartzJob) {
        quartzJobService.saveHandler(getLoginUser().getUser(), quartzJob);
        return Response.success();
    }

    /**
     * 立即执行一次定时任务
     *
     * @param id 任务id
     * @return
     */
    @ApiOperation(value = "立即执行一次定时任务")
    @PreAuthorize("hasAuthority('sys:quartz:runTask')")
    @PostMapping("/runJob")
    public Response run(@RequestParam Long id) throws SchedulerException {
        quartzJobService.run(id);
        return Response.success();
    }

    /**
     * 获得定时任务列表
     *
     * @return 定时任务列表
     */
    @ApiOperation(value = "获得定时任务列表")
    @PreAuthorize("hasAuthority('sys:quartz:list')")
    @PostMapping("/pagination")
    public Response paginationHandler(@RequestBody DataGridRequest params) {
        IPage<QuartzJob> quartzJobIPage = quartzJobService.paginate(params);
        IPage<QuartzJobVO> quartzJobVOIPage = QuartzJob2QuartzJobVOConversion.conversion(quartzJobIPage);
        return Response.success(quartzJobVOIPage);
    }


    /**
     * 删除定时任务
     *
     * @param id 任务id
     * @return
     */
    @Log(busType = LogBusType.DELETE, source = LogSourceType.MANAGE, module = LogModule.TASK)
    @ApiOperation(value = "删除定时任务")
    @PreAuthorize("hasAuthority('sys:quartz:delete')")
    @PostMapping("/delete")
    public Response deleteQuartzJobHandler(@RequestParam Long id) throws SchedulerException {
        quartzJobService.delete(getLoginUser().getUser(), id);
        return Response.success();
    }

    /**
     * 暂停定时任务
     *
     * @param id 任务id
     * @return
     */
    @Log(busType = LogBusType.FREEZE, source = LogSourceType.MANAGE, module = LogModule.TASK)
    @ApiOperation(value = "暂停定时任务")
    @PreAuthorize("hasAnyAuthority('sys:quartz:freeze')")
    @PostMapping("/pauseJob")
    public Response pause(@RequestParam Long id) throws SchedulerException {
        quartzJobService.pause(id);
        return Response.success();
    }

    /**
     * 恢复定时任务
     *
     * @param id 任务id
     * @return
     */
    @Log(busType = LogBusType.ACTIVATION, source = LogSourceType.MANAGE, module = LogModule.TASK)
    @ApiOperation(value = "恢复定时任务")
    @PreAuthorize("hasAnyAuthority('sys:quartz:active')")
    @PostMapping("/resumeJob")
    public Response resume(@RequestParam Long id) throws SchedulerException {
        quartzJobService.resume(id);
        return Response.success();
    }

    /**
     * 根据id找到任务
     *
     * @param id 任务id
     * @return
     */
    @ApiOperation(value = "根据id找到任务")
    @PostMapping("/loadMore")
    public Response loadMore(@RequestParam Long id) {
        return Response.success(quartzJobService.selectById(id));
    }

    @PostMapping("/getAllRunningJob")
    public Response getAllRunningJob() throws SchedulerException {
        return Response.success(quartzJobService.getAllRunningJob());
    }

}
